// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Y [author] rhyme_lph
// j [email]  rhymelph@gmail.com
// U [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/entities/sales_summary_entity.dart';
import 'package:finance_app/src/entities/transaction_entity.dart';
import 'package:finance_app/src/http_service/http_dashboard_service.dart';
import 'package:finance_app/src/mixin/refresh_mixin.dart';
import 'package:finance_app/src/mixin/time_sort_mixin.dart';
import 'package:finance_app/src/mixin/transaction_data_mixin.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/page/dashboard/sales/views/sales_invoice_view.dart';

import '../sales_provider.dart';

class SalesInvoiceProvider extends BaseProvider<SalesSummaryEntity>
    with
        TimeSortMixin,
        TransactionDataMixin,
        RefreshMixin<SalesSummaryEntity, List<TransactionEntity>> {
  ScrollController controller = ScrollController();

  @override
  void initState() {
    super.initState();
    controller.addListener(() {
      getParentProvider<SalesProvider>().handleScrollUpdate(controller);
    });
  }

  @override
  Future get dataFuture => HttpDashboardService.dashboardSales();

  @override
  void unFocus() {}

  @override
  void onTransactionDeleted() {
    onRefresh();
  }

  void onNavUnpaidInvoice() {
    Routes.navigateTo(RouterGenProviders.allTransactionsPage, params: {
      'type': 2,
      'initFilter': 'Unpaid',
    });
  }

  void onNavOverdueInvoice() {
    Routes.navigateTo(RouterGenProviders.allTransactionsPage, params: {
      'type': 2,
      'initFilter': 'Overdue',
    });
  }

  void onNavPaidInvoice() {
    Routes.navigateTo(RouterGenProviders.allTransactionsPage, params: {
      'type': 2,
      'initFilter': 'Paid',
    });
  }

  void onNavViewAll() {
    Routes.navigateTo(RouterGenProviders.allTransactionsPage, params: {
      'type': 2,
    });
  }

  @override
  List<String> get types => ['invoice'];

  @override
  List<TransactionEntity> get dataList => transactionList;

  @override
  Future<void> onRefresh() async {
    await post(dataFuture, onSuccess: (result) {
      data = getEntityFromResult<SalesSummaryEntity>(result);
      update();
    }, autoHandler: AutoHandler.Toast);
    return super.onRefresh();
  }
}
